// setwig.cpp
#include "setwig.h"
#include "ui_setwig.h"
#include "globalval.h"
#include "mqtthandler.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QDebug>

SetWig::SetWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SetWig),
    isMqttConnected(false),
    currentMode(1),
    overlayLabel(nullptr)
{
    ui->setupUi(this);

    // 创建覆盖层
    createOverlay();

    ui->comboBox->clear();
    ui->comboBox->addItem("循环");
    ui->comboBox->addItem("对比");
    ui->comboBox->addItem("刷新");
    ui->comboBox->setCurrentIndex(0);

    ui->pushButton->setEnabled(false);
    ui->pushButton->setStyleSheet("background-color: gray; color: white;");
    ui->pushButton->setText("未连接");

    connect(ui->pushButton, &QPushButton::clicked, this, &SetWig::onModeSelected);
    connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboBoxChanged(int)));
}

SetWig::~SetWig()
{
    destroyOverlay();
    delete ui;
}

void SetWig::createOverlay()
{
    if (overlayLabel) {
        destroyOverlay();
    }

    qDebug() << "[SetWig]: 创建覆盖层";

    overlayLabel = new QLabel(this);
    overlayLabel->setGeometry(0, 0, this->width(), this->height());
    // 统一覆盖层样式
    overlayLabel->setStyleSheet("background-color: rgba(128, 128, 128, 150); color: red; font-size: 70px; font-weight: bold;");
    overlayLabel->setAlignment(Qt::AlignCenter);
    overlayLabel->setText("设备未连接");
    overlayLabel->setVisible(true);
    overlayLabel->raise();
}

void SetWig::destroyOverlay()
{
    qDebug() << "[SetWig]: 销毁覆盖层";

    if (overlayLabel) {
        overlayLabel->setVisible(false);
        overlayLabel->deleteLater();
        overlayLabel = nullptr;
    }
}

void SetWig::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);

    if (overlayLabel) {
        overlayLabel->setGeometry(0, 0, this->width(), this->height());
        overlayLabel->raise();
    }
}

// 修改 setMqttConnected 方法
void SetWig::setMqttConnected(bool connected)
{
    isMqttConnected = connected;

    if (connected) {
        destroyOverlay();
        ui->pushButton->setEnabled(true);

        // 强制设置为循环模式（索引0对应模式1）
        ui->comboBox->setCurrentIndex(0);
        currentMode = 1;  // 循环模式

        // 更新按钮显示为循环模式已确认
        QString modeText = "循环模式已确认";
        ui->pushButton->setText(modeText);
        ui->pushButton->setStyleSheet("background-color: green; color: white;");

        // 发出模式改变信号（循环模式）
        emit modeChanged(1);

        qDebug() << "SetWig: MQTT连接成功，默认选择循环模式";
    } else {
        if (!overlayLabel) {
            createOverlay();
        }
        ui->pushButton->setStyleSheet("background-color: gray; color: white;");
        ui->pushButton->setText("未连接");
        ui->pushButton->setEnabled(false);
        qDebug() << "SetWig: MQTT连接断开，模式选择功能已禁用";
    }
}

// 发送当前模式
void SetWig::sendCurrentMode()
{
    if (isMqttConnected) {
        int selectedMode = ui->comboBox->currentIndex() + 1;
        sendModeToMqtt(selectedMode);
    }
}

// 模式选择按钮点击槽函数
void SetWig::onModeSelected()
{
    if (!isMqttConnected) {
        qDebug() << "SetWig: MQTT未连接，无法发送模式选择";
        return;
    }

    int selectedMode = ui->comboBox->currentIndex() + 1;

    // 更新当前模式
    currentMode = selectedMode;

    // 发送模式到MQTT
    sendModeToMqtt(currentMode);

    // 发出模式改变信号
    emit modeChanged(currentMode);

    qDebug() << "SetWig: 模式已选择:" << selectedMode;
}

// 下拉框变化槽函数
void SetWig::onComboBoxChanged(int index)
{
    int selectedMode = index + 1;
    QString modeText;

    switch (selectedMode) {
    case 1:
        modeText = "循环模式";
        break;
    case 2:
        modeText = "对比模式";
        break;
    case 3:
        modeText = "刷新模式";
        break;
    default:
        modeText = "未知模式";
        break;
    }

    if (isMqttConnected) {
        ui->pushButton->setText(modeText + " - 点击确认");
        ui->pushButton->setStyleSheet("background-color: orange; color: white;");
    }

    // 发出模式改变信号
    emit modeChanged(selectedMode);

    qDebug() << "SetWig: 模式已更改为:" << modeText;
}

// 发送模式到MQTT
void SetWig::sendModeToMqtt(int mode)
{
    if (!GlobalVal::mqtt) {
        qDebug() << "SetWig: MQTT客户端未初始化";
        return;
    }

    // 创建JSON消息
    QJsonObject jsonObj;
    jsonObj["code"] = mode;

    QJsonDocument doc(jsonObj);
    QByteArray message = doc.toJson();

    // 使用MQTT处理器中的主题定义
    QMQTT::Message mqttMessage;
    mqttMessage.setTopic("MQTT  rec");  // 使用正确的发送主题
    mqttMessage.setPayload(message);

    GlobalVal::mqtt->publish(mqttMessage);

    // 更新按钮状态为已确认
    QString modeText;
    switch (mode) {
    case 1:
        modeText = "循环模式已确认";
        break;
    case 2:
        modeText = "对比模式已确认";
        break;
    case 3:
        modeText = "再次点击获取刷新";
        break;
    default:
        modeText = "模式已确认";
        break;
    }

    ui->pushButton->setText(modeText);
    ui->pushButton->setStyleSheet("background-color: green; color: white;");

    qDebug() << "SetWig: 发送模式选择消息:" << QString(message);
}

